package code.theMaze;

import listeners.ColorSwitcher;
import listeners.Filelooker;
import listeners.LoadButtonListener;
import listeners.NewActivitygenerator;
import utilities.Workbench;
import android.app.Activity;
import android.graphics.Color;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class TheMazeView extends ScrollView {

	private Activity theMazeView;
	private LinearLayout layout1, layout2;
	private TextView titel;
	private TextView console;
	private Workbench workbench;
	private Button redButton, blueButton, yellowButton, greenButton,
			chosencolor, load, activity, filelooker;

	public TheMazeView(Activity context) {
		super(context);

		this.workbench = (Workbench) context.getApplication();

		this.initialisiseTheMemory();

		this.theMazeView = context;
		this.setBackgroundColor(Color.LTGRAY);

		layout1 = new LinearLayout(theMazeView);
		layout1.setOrientation(LinearLayout.VERTICAL);
		addView(layout1);

		titel = new TextView(theMazeView);
		titel.setText("the maze without xml");
		this.layout1.addView(titel);

		for (int i = 0; i < workbench.getMazeHeight(); i++) {
			LinearLayout ll = new LinearLayout(theMazeView);
			ll.setOrientation(LinearLayout.HORIZONTAL);
			for (int j = 0; j < workbench.getMazeWith(); j++) {
				ll.addView(new TheMazeButton(theMazeView, workbench, j, i));
			}
			layout1.addView(ll);
		}

		layout2 = new LinearLayout(theMazeView);
		layout2.setOrientation(LinearLayout.HORIZONTAL);
		layout2.setPadding(0, 10, 0, 0);

		layout1.addView(layout2);

		redButton = new Button(theMazeView);
		redButton.setText("red");
		redButton.setOnClickListener(new ColorSwitcher(workbench, redButton));
		layout2.addView(redButton);

		greenButton = new Button(theMazeView);
		greenButton.setText("green");
		greenButton
				.setOnClickListener(new ColorSwitcher(workbench, greenButton));
		layout2.addView(greenButton);

		blueButton = new Button(theMazeView);
		blueButton.setText("blue");
		blueButton.setOnClickListener(new ColorSwitcher(workbench, blueButton));
		layout2.addView(blueButton);

		yellowButton = new Button(theMazeView);
		yellowButton.setText("yellow");
		yellowButton.setOnClickListener(new ColorSwitcher(workbench,
				yellowButton));
		layout2.addView(yellowButton);

		chosencolor = new Button(theMazeView);
		chosencolor.setBackgroundColor(workbench.getTheChosenColor());
		chosencolor.setClickable(false);
		chosencolor.setHeight(30);
		chosencolor.setWidth(30);
		chosencolor.setPadding(30, 0, 0, 0);
		workbench.setTheChosenColorButton(chosencolor);
		layout2.addView(chosencolor);

		workbench.setMainActivity(this.theMazeView);

		activity = new Button(theMazeView);
		activity.setText("save");
		activity.setOnClickListener(new NewActivitygenerator(workbench,
				activity));
		layout1.addView(activity);

		this.load = new Button(theMazeView);
		this.load.setText("load");
		this.load.setOnClickListener(new LoadButtonListener(workbench));
		layout1.addView(this.load);
		
		this.filelooker = new Button(theMazeView);
		this.filelooker.setText("file");
		this.filelooker.setOnClickListener(new Filelooker(workbench, this.theMazeView));
		layout1.addView(this.filelooker);
		
		console = new TextView(theMazeView);
		console.setTextColor(Color.BLACK);
		console.setText("console");
		workbench.setConsole(console);
		layout1.addView(workbench.getConsole());
		

	}

	private void initialisiseTheMemory() {

		int[][] arr = new int[workbench.getMazeWith()][];
		for (int i = 0; i < workbench.getMazeWith(); i++) {
			arr[i] = new int[workbench.getMazeHeight()];

			workbench.setMemory(arr);
		}

	}
}
